/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.losalpes.catalogo.beans;

import com.losalpes.catalogo.bos.Cliente;
import com.losalpes.catalogo.delegate.TiendaLosAlpesBussinesDelegate;
import java.util.ArrayList;
import java.util.List;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlCommandLink;
import javax.faces.model.SelectItem;

/**
 *
 * @author AndhyMao
 */

public class ClientBean {

    private Cliente cliente = new Cliente();
    private List<SelectItem> clientesCB = new ArrayList<SelectItem>();
    private List<Cliente> listClientes = new ArrayList<Cliente>();
    private HtmlCommandLink commandLink1 = new HtmlCommandLink();
    private HtmlCommandLink commandLink2 = new HtmlCommandLink();

    /** Creates a new instance of ClientBean */
    public ClientBean() {
        TiendaLosAlpesBussinesDelegate.getInstance();
    }

    /**
     * @return the cliente
     */
    public Cliente getCliente() {
        return cliente;
    }

    /**
     * @param cliente the cliente to set
     */
    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    /**
     * @return the listClientes
     */
    public List<Cliente> getListClientes() {
        try {
            listClientes = TiendaLosAlpesBussinesDelegate.getInstance().findAllClientes();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return listClientes;
    }

    /**
     * @param listClientes the listClientes to set
     */
    public void setListClientes(List<Cliente> listClientes) {
        this.listClientes = listClientes;
    }

    public String load() {
        for (UIComponent uc : commandLink1.getChildren()) {
            System.out.print("uc:id: " + uc.getId());
            if (uc.getId().equals("inputHidden1")) {
                ValueExpression v = uc.getValueExpression("value");
            }
        }
        return null;
    }

    public String add() {
        try {
            TiendaLosAlpesBussinesDelegate.getInstance().addClient(cliente);
            cliente = new Cliente();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String edit() {
        return null;
    }

    public String delete() {
        System.out.println("Entre delete ...");
        return null;
    }

    /**
     * @return the commandLink1
     */
    public HtmlCommandLink getCommandLink1() {
        return commandLink1;
    }

    /**
     * @param commandLink1 the commandLink1 to set
     */
    public void setCommandLink1(HtmlCommandLink commandLink1) {
        this.commandLink1 = commandLink1;
    }

    /**
     * @return the commandLink2
     */
    public HtmlCommandLink getCommandLink2() {
        return commandLink2;
    }

    /**
     * @param commandLink2 the commandLink2 to set
     */
    public void setCommandLink2(HtmlCommandLink commandLink2) {
        this.commandLink2 = commandLink2;
    }

    public List<SelectItem> getClientesCB() {

        SelectItem clienteCB;

        for(Cliente cliente: getListClientes()) {
            clienteCB = new SelectItem(cliente.getNombre(), cliente.getCustomerCode());
            clientesCB.add(clienteCB);
        }
        return clientesCB;
    }

    public void setClientesCB(List<SelectItem> clientesCB) {
        this.clientesCB = clientesCB;
    }
    
}
